// pages/pinche/issue/issue.js
var util = require('../../../utils/util.js');
var app = getApp();
var today = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 1))).split(' ')[0];
var minday = util.formatTime(new Date()).split(' ')[0];
var maxday = util.formatTime(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24 * 62))).split(' ')[0];

Page({

  /**
   * 页面的初始数据
   */
  data: {
      location: ['请选择', '邯郸', '张江','枫林','江湾'],
      startLocation: 0,
      endLocation: 0,
      targetNum : 1,
      date: today,
      start: minday,
      end: maxday,
      time: '12:00'
  },

  setStartLocation: function(e) {
    this.setData({
      startLocation: e.detail.value
    });
  },

  setEndLocation: function (e) {
    this.setData({
      endLocation: e.detail.value
    });
  },

  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  formSubmit: function (e) {
    var data = e.detail.value;
    var that = this;
    console.log(data);

    if((data.startLocation == "0") || (data.endLocation == "0")){
      util.isError("请选择出发地或目的地",that);
      return false;
    }
    if (data.startLocation == data.endLocation) {
      util.isError("出发地不能与目的地相同",that);
      return false;
    }
    //发送信息
    if(app.globalData.openId == "") {
      util.isError("对不起，出了点小问题哦，请重试！", that);
      return false;
    }
    wx.request({
      url: 'https://myid',
      data: {
        openId: app.globalData.openId,
        startLocation: data.startLocation,
        endLocation: data.endLocation,
        date: data.date,
        time: data.time
      },
      method: "POST",
      header: {
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionkey')
        //'Cookie': 'JSESSIONID=D930710CA204EF994D87456D0B2EB7F6'
      },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          wx.showToast({
            title: '发布成功',
          });
          wx.navigateBack({
            delta: 1 //返回的页面数
          });
        }
        else {
          wx.showModal({
            title: '发布失败',
            content: "请稍后重试！"
          });
        }
      }
    })
    util.clearError(that);
  }
})